﻿
var formJson = new Object();
var lastestPopup1Link = '';
var receive_person = '';
var receive_person_staus = false;
var receive_person_link = '';
formJson.type = '';
formJson.status = '';
formJson.channel_id = '';
formJson.objective_id = '';

function goToPopup2(link, formData) {
    receive_person_link = link;
    receive_person_staus = true;
    formJson = formData;
    $.colorbox.close();
}

function GetRootChain(val, type, text) {

    if (type == 'lovchannelin') {
        formJson.channel_id = val;
        formJson.channel_text = text;
        //console.log(formJson.channel_text);
    }

    if (type == 'lovobject') {
        formJson.objective_id = val;
        formJson.objective_text = text;
        //console.log(formJson.objective_text);
    }

    if (type == 'type') {
        formJson.type = val;
        formJson.type_text = text;
        //console.log(formJson.type_text);
    }

    if (type == 'operstatus') {
        formJson.status = val;
        formJson.status_text = text;
        //console.log(formJson.status_text);
    }
}
function callcolorbox2(link) { 
    lastestPopup1Link = link;
    $.colorbox({
        iframe: true,
        width: "650px",
        height: "540px",
        href: lastestPopup1Link,
        fastIframe: false,
        onClosed: function () {
            if (parent.receive_person_staus) {
                $.colorbox({
                    iframe: true,
                    width: "650px",
                    height: "550px",
                    href: parent.receive_person_link,
                    fastIframe: false,
                    onClosed: function () {
                        callcolorbox2(lastestPopup1Link);
                    }
                });

                parent.receive_person_staus = false;
            } else {
                parent.formJson = null;
            }
        }
    });
}

var formJson = Object();
var link_temporary_multi_levels = Array(); 
var link_level_cursor = -1;
var redirectLink = '';
function callcolorboxrecursive(nextlink, datasInForm, goprevlink, specialtype, updatepriviouslink) {
    var messageDebug = "nextlink=" + nextlink + "&datasInForm=" + datasInForm + "&goprevlink=" + goprevlink +
        "&specialtype=" + specialtype + ',updatepriviouslink=' + updatepriviouslink + "\n\n";
    try {
        if (updatepriviouslink) {
           //  link_temporary_multi_levels[link_temporary_multi_levels.length - 1] = updatepriviouslink;
        }

        if (nextlink /*|| specialtype != undefined*/) {
            // ส่ง link ชัดถัดไปมาผ่านทาง parameter
            link_level_cursor++;
            link_temporary_multi_levels[link_level_cursor] = /*specialtype!=undefined ? getCountryLink(specialtype) :*/ nextlink;
            redirectLink = link_temporary_multi_levels[link_level_cursor];
            messageDebug += 'เดินหน้าไป ' + link_level_cursor + ': ' + redirectLink;
            //alert('nextlink => ' + messageDebug);
        } else if (goprevlink && link_level_cursor >= 1) { 
            link_level_cursor--;
            link_temporary_multi_levels.pop();
            redirectLink= link_temporary_multi_levels[link_level_cursor];
            messageDebug += 'ถอยหลังไป ' + link_level_cursor + ': ' + redirectLink;
            //alert('goprevlink && link_level_cursor >= 1 => ' + messageDebug);
        } else if (goprevlink && link_level_cursor < 1) {
            redirectLink = '';
            messageDebug += 'ม่มีลิงค์ถอย';
            if (parent && parent.formJson) {
                parent.formJson = Object();
            }

            if (!parent && formJson) {
                formJson = Object();
            }
            //alert('goprevlink && link_level_cursor < 1 => ' + messageDebug);
        } else {
            redirectLink = '';
            messageDebug += 'Unknown Action';
            if (parent && parent.formJson) {
                parent.formJson = Object();
            }

            if (!parent && formJson) {
                formJson = Object();
            }
            //alert('else => ' + messageDebug);
        } 
    }catch(ex){
        alert('Error ' + ex);
    }

    messageDebug += "\nจำนวน Link มีทั้งหมด " + link_temporary_multi_levels.length + " ชั้น\n";
    for (i = 0 ; i < link_temporary_multi_levels.length; i++) {
        messageDebug += '[' + i + ']' + link_temporary_multi_levels[i] + "\n";
    }

    if (!redirectLink) {
        return;
    }

    //alert('messageDebug => ' + messageDebug);

    $.colorbox({
        iframe: true,
        fastIframe: false,
        width: "650px",
        height: "540px",
        href: redirectLink,
        onClosed: function () {
            callcolorboxrecursive(null, null, true);
        }
    });

}  
function getRedirectLink() {
    alert('will go redirectlink => ' + redirectLink);
    return redirectLink;
}
function addTextLocation2(locationToRedirect, newHref) {
    formJson.district_achor = newHref;
    formJson.locationToRedirect = locationToRedirect; 
    //searchPlace();
}